package com.example.duckshop.ui.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.duckshop.MyApplication;
import com.example.duckshop.R;
import com.example.duckshop.ui.fragment.HomeFragment;
import com.example.duckshop.ui.fragment.OrderFragment;
import com.example.duckshop.ui.fragment.UserFragment;
import com.example.duckshop.util.SPUtils;
import com.example.duckshop.widget.ActionBar;


/**
 * 首页
 */
public class MainActivity extends Activity {
    private ActionBar mActionBar;//标题栏
    private Activity myActivity;
    private LinearLayout llContent;
    private RadioButton rbShop;
    private RadioButton rbOrder;
    private RadioButton rbUser;
    private Fragment[] fragments = new Fragment[]{null, null,null};//存放Fragment

    private String mAccount;//账号
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myActivity = this;
        setContentView(R.layout.activity_main);
        MyApplication.Instance.setMainActivity(myActivity);
        llContent =  (LinearLayout) findViewById(R.id.ll_main_content);
        rbShop = (RadioButton) findViewById(R.id.rb_main_shop);
        rbOrder = (RadioButton) findViewById(R.id.rb_main_order);
        rbUser = (RadioButton) findViewById(R.id.rb_main_user);
        mActionBar = (ActionBar) findViewById(R.id.myActionBar);
        //菜单
        mActionBar.setData(myActivity,"首页", 0, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
            }

            @Override
            public void onRightClick() {
            }
        });
        initView();
        setViewListener();
    }

    //通过mActionBar.setTitle方法改变ActionBar的标题，然后通过switchFragment方法切换相应的Fragment
    private void setViewListener() {
        rbShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionBar.setTitle("首页");
                switchFragment(0);
            }
        });
        rbOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionBar.setTitle("收藏");
                switchFragment(1);
            }
        });
        rbUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionBar.setTitle("我的");
                switchFragment(2);
            }
        });
    }


    private void initView() {
        //设置导航栏图标样式
        //getResources方法获取到图片资源并转换成Drawable对象。接着通过setBounds方法设置图标的边距和大小。
        Drawable iconNews=getResources().getDrawable(R.drawable.selector_main_rb_shop);//设置主页图标样式
        iconNews.setBounds(0,0,68,68);//设置图标边距 大小
        // 然后调用setCompoundDrawables方法设置图标的位置，null表示不作处理，依然保留原来的位置。
        rbShop.setCompoundDrawables(null,iconNews,null,null);
        // 最后通过setCompoundDrawablePadding方法设置文字与图片之间的间距。
        rbShop.setCompoundDrawablePadding(5);//设置文字与图片的间距
        Drawable iconOrder=getResources().getDrawable(R.drawable.selector_main_rb_order);//设置主页图标样式
        iconOrder.setBounds(0,0,68,68);//设置图标边距 大小
        rbOrder.setCompoundDrawables(null,iconOrder,null,null);//设置图标位置
        rbOrder.setCompoundDrawablePadding(5);//设置文字与图片的间距
        Drawable iconUser=getResources().getDrawable(R.drawable.selector_main_rb_user);//设置主页图标样式
        iconUser.setBounds(0,0,68,68);//设置图标边距 大小
        rbUser.setCompoundDrawables(null,iconUser,null,null);//设置图标位置
        rbUser.setCompoundDrawablePadding(5);//设置文字与图片的间距
        //最后还调用了switchFragment方法切换到默认的Fragment，以及setChecked方法设置默认选中的RadioButton为rbShop。
        switchFragment(0);
        rbShop.setChecked(true);
    }
    /**
     * 方法 - 切换Fragment
     *
     * @param fragmentIndex 要显示Fragment的索引
     */
    private void switchFragment(int fragmentIndex) {
        //在Activity中显示Fragment
        //1、获取Fragment管理器 FragmentManager
        FragmentManager fragmentManager = this.getFragmentManager();
        //2、开启fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //如果需要显示的Fragment为null，就new。并添加到Fragment事务中
        if (fragments[fragmentIndex] == null) {
            switch (fragmentIndex) {
                case 0://NewsFragment
                    fragments[fragmentIndex] = new HomeFragment();
                    break;
                case 1://UserManageFragment
                    fragments[fragmentIndex] = new OrderFragment();
                    break;
                case 2://UserFragment
                    fragments[fragmentIndex] = new UserFragment();
                    break;
            }
            //添加Fragment对象到Fragment事务中
            //参数：显示Fragment的容器的ID，Fragment对象
            transaction.add(R.id.ll_main_content, fragments[fragmentIndex]);
        }

        //隐藏其他的Fragment
        for (int i = 0; i < fragments.length; i++) {
            if (fragmentIndex != i && fragments[i] != null) {
                //隐藏指定的Fragment
                transaction.hide(fragments[i]);
            }
        }
        //4、显示Fragment
        transaction.show(fragments[fragmentIndex]);

        //5、提交事务
        transaction.commit();
    }
    /**
     * 双击退出
     */
    //当用户按下返回键时，如果两次按下时间间隔小于2秒，则退出应用程序，否则弹出一个提示。
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
        }

        return false;
    }

    private long time = 0;

    public void exit() {
        if (System.currentTimeMillis() - time > 2000) {
            time = System.currentTimeMillis();
            Toast.makeText(myActivity,"再点击一次退出应用程序", Toast.LENGTH_LONG).show();
        } else {
            finish();
        }
    }
}
